﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFM.API;
using CFM.DTO;
using CFM.Common;
using System.Text.RegularExpressions;

namespace CFM
{
    public partial class frmCategory : Form
    {
        private CategoriesAPI _api = null;
        public frmCategory()
        {
            InitializeComponent();
            _api = new CategoriesAPI();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableControl(true);
            ResetControl();

            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtCategoryID.Text.Length > 0)
            {
                _api.Delete(txtCategoryID.Text.Trim());
                BindDataGridView();
                ResetControl();

                btnDelete.Enabled = false;
                btnEdit.Enabled = false;
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtCategoryID.Text.Length > 0)
            {
                EnableControl(true);
                txtCategoryID.Enabled = false;
                MethodHelper.ClearErrorProvider();

                btnSave.Enabled = true;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckBeforeSave())
                return;
            Categories cate = new Categories();
            cate.CategoryId = txtCategoryID.Text;
            cate.CategoryName = txtCategoryName.Text;

            if (txtCategoryID.Enabled)
                _api.Create(cate);
            else
                _api.Update(cate);
            BindDataGridView();
            ResetControl();
            MethodHelper.ClearErrorProvider();
        }

        private void frmCategory_Load(object sender, EventArgs e)
        {
            EnableControl(false);

            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            btnSave.Enabled = false;

            BindDataGridView();
        }

        private void BindDataGridView()
        {
            grvCategory.DataSource = _api.GetAll();
        }
        
        private void ResetControl()
        {
            txtCategoryName.Text = string.Empty;
            txtCategoryID.Text = string.Empty;
            btnSave.Enabled = true;
            btnDelete.Enabled = false;
            btnEdit.Enabled = false;
            txtCategoryName.Focus();
            MethodHelper.ClearErrorProvider();
        }
        private void EnableControl(bool flag)
        {
            txtCategoryID.Enabled = flag;
            txtCategoryName.Enabled = flag;
        }

        private bool CheckBeforeSave()
        {
            bool flag = true;
            if (txtCategoryID.Enabled && txtCategoryID.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtCategoryID, Const.EMPTY_MESSAGE);
                flag = false;
            }
            if (txtCategoryName.Enabled && txtCategoryName.Text.Length == 0)
            {
                MethodHelper.AddErrorProvider(txtCategoryName, Const.EMPTY_MESSAGE);

                flag = false;
            }
            if (!txtCategoryName.Enabled && !txtCategoryID.Enabled)
                flag = false;
            return flag;
        }

        private void txtCategoryName_TextChanged(object sender, EventArgs e)
        {
            if (txtCategoryName.Enabled == true&&txtCategoryID.Enabled==true)
            {
                txtCategoryID.Text = MethodHelper.GetIdFromName(txtCategoryName.Text);
            }
        }

        private void grvCategory_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
                return;
            Categories cate = grvCategory.Rows[e.RowIndex].DataBoundItem as Categories;
            txtCategoryID.Text = cate.CategoryId;
            txtCategoryName.Text = cate.CategoryName;
            EnableControl(false);
            MethodHelper.ClearErrorProvider();

            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            btnSave.Enabled = false;
        }
    }
}
